package com.ruoyi.statistics.service.impl;

import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.domain.LabOrgnizationSecond;
import com.ruoyi.labdata.domain.LabOrgnizationThird;
import com.ruoyi.labdata.service.ILabOrgnizationSecondService;
import com.ruoyi.labdata.service.ILabOrgnizationService;
import com.ruoyi.labdata.service.ILabOrgnizationThirdService;
import com.ruoyi.statistics.domain.LabStatisticsBaseCollege;
import com.ruoyi.statistics.domain.LabStatisticsEquipment;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.statistics.mapper.LabStatisticsLaboratoryMapper;
import com.ruoyi.statistics.domain.LabStatisticsLaboratory;
import com.ruoyi.statistics.service.ILabStatisticsLaboratoryService;

/**
 * 实验室基本情况Service业务层处理
 * 
 * @author jun.wang
 * @date 2022-05-21
 */
@Service
public class LabStatisticsLaboratoryServiceImpl implements ILabStatisticsLaboratoryService 
{
    @Autowired
    private LabStatisticsLaboratoryMapper labStatisticsLaboratoryMapper;

    @Autowired
    private ILabOrgnizationSecondService labOrgnizationSecondService;

    @Autowired
    private ILabOrgnizationThirdService labOrgnizationThirdService;

    /**
     * 查询实验室基本情况
     * 
     * @param id 实验室基本情况ID
     * @return 实验室基本情况
     */
    @Override
    public LabStatisticsLaboratory selectLabStatisticsLaboratoryById(Long id)
    {
        return labStatisticsLaboratoryMapper.selectLabStatisticsLaboratoryById(id);
    }

    /**
     * 查询实验室基本情况列表
     * 
     * @param labStatisticsLaboratory 实验室基本情况
     * @return 实验室基本情况
     */
    @Override
    public List<LabStatisticsLaboratory> selectLabStatisticsLaboratoryList(LabStatisticsLaboratory labStatisticsLaboratory)
    {
        return labStatisticsLaboratoryMapper.selectLabStatisticsLaboratoryList(labStatisticsLaboratory);
    }

    /**
     * 新增实验室基本情况
     * 
     * @param labStatisticsLaboratory 实验室基本情况
     * @return 结果
     */
    @Override
    public int insertLabStatisticsLaboratory(LabStatisticsLaboratory labStatisticsLaboratory)
    {
        return labStatisticsLaboratoryMapper.insertLabStatisticsLaboratory(labStatisticsLaboratory);
    }

    /**
     * 修改实验室基本情况
     * 
     * @param labStatisticsLaboratory 实验室基本情况
     * @return 结果
     */
    @Override
    public int updateLabStatisticsLaboratory(LabStatisticsLaboratory labStatisticsLaboratory)
    {
        labStatisticsLaboratory.setUpdateTime(DateUtils.getNowDate());
        return labStatisticsLaboratoryMapper.updateLabStatisticsLaboratory(labStatisticsLaboratory);
    }

    /**
     * 批量删除实验室基本情况
     * 
     * @param ids 需要删除的实验室基本情况ID
     * @return 结果
     */
    @Override
    public int deleteLabStatisticsLaboratoryByIds(Long[] ids)
    {
        return labStatisticsLaboratoryMapper.deleteLabStatisticsLaboratoryByIds(ids);
    }

    /**
     * 删除实验室基本情况信息
     * 
     * @param id 实验室基本情况ID
     * @return 结果
     */
    @Override
    public int deleteLabStatisticsLaboratoryById(Long id)
    {
        return labStatisticsLaboratoryMapper.deleteLabStatisticsLaboratoryById(id);
    }

  @Override
  public List<LabStatisticsLaboratory> selectLabStatisticsLaboratoryByBaseCollegeId(
      Long baseCollegeId) {
    return labStatisticsLaboratoryMapper.selectLabStatisticsLaboratoryByBaseCollegeId(baseCollegeId);
  }

    @Override
    public void addLaboratory(LabStatisticsBaseCollege baseCollege, List<LabOrgnization> labOrgnizations) {
        Long baseCollegeId = baseCollege.getId();
        deleteLabStatisticsLaboratoryByBaseCollegeId(baseCollegeId);

        for (LabOrgnization labOrgnization : labOrgnizations) {

//            LabOrgnizationSecond labOrgnizationSecond = labOrgnizationSecondService.selectLabOrgnizationSecondById(labOrgnization.getOrgId());
            LabOrgnizationThird labOrgnizationThird = labOrgnizationThirdService.selectLabOrgnizationThirdById(labOrgnization.getOrgId());

            LabStatisticsLaboratory laboratory = new LabStatisticsLaboratory();
            laboratory.setBaseCollegeId(baseCollegeId);
            laboratory.setLabId(labOrgnization.getOrgId());
            laboratory.setLabCode(labOrgnization.getOrgCode());
            laboratory.setLabName(labOrgnization.getOrgName());

            laboratory.setLabCategory(null); // 实验室类别
            laboratory.setBuildYear(labOrgnization.getCreateYear()); // 建立年份
            laboratory.setUsablearea(labOrgnizationThird.getUseArea()); // 房屋使用面积
            laboratory.setLabType(labOrgnizationThird.getLabTypeOpt()); // 实验室类型
            laboratory.setSubject(labOrgnizationThird.getBelongSubjectOpt()); // 所属学科

            insertLabStatisticsLaboratory(laboratory);
        }

    }

  @Override
  public int deleteLabStatisticsLaboratoryByBaseCollegeId(Long baseCollegeId) {

        return labStatisticsLaboratoryMapper.deleteLabStatisticsLaboratoryByBaseCollegeId(baseCollegeId);
  }
}
